package com.ittao.taoaicodecommon.manage.storage.lifecycle.tracker;

import com.ittao.taoaicodecommon.manage.storage.lifecycle.observer.SessionActivityEvent;
import com.ittao.taoaicodecommon.manage.storage.lifecycle.observer.SessionActivityObserver;
import com.ittao.taoaicodecommon.manage.storage.lifecycle.observer.SessionActivitySubject;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 会话活动跟踪器
 * 负责管理会话活动观察者并发布会话活动事件
 *
 * 核心功能：
 * 1. 管理观察者的注册和注销
 * 2. 发布会话活动事件给所有观察者
 * 3. 提供便捷的事件发布方法
 */
@Slf4j
@Component
public class SessionActivityTracker {

    private final SessionActivitySubject subject;
    private final List<SessionActivityObserver> observers;

    public SessionActivityTracker(List<SessionActivityObserver> observers) {
        this.subject = new SessionActivitySubject(Runnable::run); // 使用同步执行器进行测试
        this.observers = observers;
    }

    @PostConstruct
    public void init() {
        // 注册所有观察者
        for (SessionActivityObserver observer : observers) {
            subject.addObserver(observer);
            log.info("注册会话活动观察者: {}", observer.getObserverName());
        }

        log.info("会话活动跟踪器初始化完成，已注册观察者数量: {}", observers.size());
    }

    /**
     * 发布会话活动事件
     */
    public void publishEvent(SessionActivityEvent event) {
        try {
            subject.notifyObservers(event);
            log.debug("发布会话活动事件: sessionId={}, activityType={}, userId={}",
                    event.getSessionId(), event.getActivityType(), event.getUserId());
        } catch (Exception e) {
            log.error("发布会话活动事件失败: sessionId={}, activityType={}, error={}",
                    event.getSessionId(), event.getActivityType(), e.getMessage(), e);
        }
    }

    /**
     * 发布项目生成开始事件
     */
    public void publishProjectGenerationStart(String sessionId, String userId, String appId, String tempFilePath) {
        SessionActivityEvent event = SessionActivityEvent.builder()
                .sessionId(sessionId)
                .userId(userId)
                .appId(appId)
                .activityType(SessionActivityEvent.ActivityType.PROJECT_GENERATION_START)
                .eventTime(LocalDateTime.now())
                .tempFilePath(tempFilePath)
                .build();

        publishEvent(event);
    }

    /**
     * 发布项目生成完成事件
     */
    public void publishProjectGenerationComplete(String sessionId, String userId, String appId, String tempFilePath) {
        SessionActivityEvent event = SessionActivityEvent.builder()
                .sessionId(sessionId)
                .userId(userId)
                .appId(appId)
                .activityType(SessionActivityEvent.ActivityType.PROJECT_GENERATION_COMPLETE)
                .eventTime(LocalDateTime.now())
                .tempFilePath(tempFilePath)
                .build();

        publishEvent(event);
    }

    /**
     * 发布项目修改事件
     */
    public void publishProjectModification(String sessionId, String userId, String appId, String tempFilePath) {
        SessionActivityEvent event = SessionActivityEvent.builder()
                .sessionId(sessionId)
                .userId(userId)
                .appId(appId)
                .activityType(SessionActivityEvent.ActivityType.PROJECT_MODIFICATION)
                .eventTime(LocalDateTime.now())
                .tempFilePath(tempFilePath)
                .build();

        publishEvent(event);
    }
}